#!/bin/bash

# 定义颜色输出函数
red() { echo -e "\033[31m\033[01m[WARNING] $1\033[0m"; }
green() { echo -e "\033[32m\033[01m[INFO] $1\033[0m"; }
greenline() { echo -e "\033[32m\033[01m $1\033[0m"; }
yellow() { echo -e "\033[33m\033[01m[NOTICE] $1\033[0m"; }
blue() { echo -e "\033[34m\033[01m[MESSAGE] $1\033[0m"; }
light_magenta() { echo -e "\033[95m\033[01m[NOTICE] $1\033[0m"; }
highlight() { echo -e "\033[32m\033[01m$1\033[0m"; }
cyan() { echo -e "\033[38;2;0;255;255m$1\033[0m"; }

# 声明系统相关的变量
ARCH=""
OS=""

# 检查是否以 root 用户身份运行
if [ "$(id -u)" -ne 0 ]; then
    green "注意！输入密码过程不显示*号属于正常现象"
    echo "此脚本需要以 root 用户权限运行，请输入当前用户的密码："
    # 使用 'sudo' 重新以 root 权限运行此脚本
    sudo -E "$0" "$@"
    exit $?
fi

# 检查CPU架构
if [[ "$(uname -i)" == "x86_64" ]]; then
    ARCH="amd64"
elif [[ "$(uname -i)" == "aarch64" ]]; then
    ARCH="arm64"
else
    red "当前CPU架构$ARCH 不被支持"
    exit 1
fi

# 检查操作系统

if [[ -f /etc/redhat-release ]]; then
    # CentOS/RHEL
    OS="centos"
elif [[ -f /etc/lsb-release ]]; then
    # Ubuntu/Debian
    OS="ubuntu"
else
    red "当前操作系统$OS 不被支持"
    exit 1
fi

declare -a menu_options
declare -A commands
menu_options=(
    "更新系统软件包"
    "从系统软件包中安装JDK8"
    "安装Docker和DockerCompose"
    "安装alist"
    "安装并启动文件管理器FileBrowser"
    "设置文件管理器开机自启动"
    "安装1panel面板管理工具"
    "查看1panel用户信息"
    "安装alist"
    "安装小雅alist"
    "安装小雅转存清理工具"
    "安装小雅tvbox"
    "使用docker-compose部署小雅全家桶(建议x86-64设备)"
    "修改阿里云盘Token(32位)"
    "修改阿里云盘OpenToken(335位)"
    "修改小雅转存文件夹ID(40位)"
    "安装内网穿透工具Cpolar"
    "安装盒子助手docker版"
    "安装特斯拉伴侣TeslaMate"
    "安装CasaOS面板"
    "安装内网穿透工具DDNSTO"
    "更新脚本"
)

commands=(
       
    ["更新系统软件包"]="update_system_packages"
     ["从系统软件包中安装JDK8"]="install_jdk8"
     ["安装Docker和DockerCompose"]="install_docker"
    ["安装并启动文件管理器FileBrowser"]="install_filemanager"
    ["设置文件管理器开机自启动"]="start_filemanager"
    ["安装1panel面板管理工具"]="install_1panel_on_linux"
    ["查看1panel用户信息"]="read_user_info"
    ["安装alist"]="install_alist"
    ["安装小雅alist"]="install_xiaoya_alist"
    ["安装小雅转存清理工具"]="install_xiaoya_keeper"
    ["修改阿里云盘Token(32位)"]="update_aliyunpan_token"
    ["修改阿里云盘OpenToken(335位)"]="update_aliyunpan_opentoken"
    ["修改小雅转存文件夹ID(40位)"]="update_aliyunpan_folder_id"
    ["安装内网穿透工具Cpolar"]="install_cpolar"
    ["安装盒子助手docker版"]="install_wukongdaily_box"
    ["安装CasaOS面板"]="install_casaos"
    ["更新脚本"]="update_scripts"
    ["安装小雅tvbox"]="install_xiaoya_tvbox"
    ["安装特斯拉伴侣TeslaMate"]="install_teslamate"
    ["安装内网穿透工具DDNSTO"]="install_ddnsto"
    ["使用docker-compose部署小雅全家桶(建议x86-64设备)"]="install_xiaoya_emby"
)

# install_jdk8
install_jdk8(){
    if [[ $OS -eq "centos" ]]; then
        sudo yum install -y java-1.8.0-openjdk-devel.x86_64
    elif [[ $OS -eq "ubuntu" ]]; then
        # Ubuntu/Debian
        sudo apt-get install -y openjdk-8-jdk-headless
    else
        echo "Unsupported operating system."
        return
    fi
}
# install_docker
install_docker(){
    # docker
    # sh -c $(curl -fsSL get.docker.com -o get-docker.sh) --mirror Aliyun
    mkdir -p  /usr/local/bin
    curl -L https://gitee.com/yookoo/shell/raw/master/install/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
}

show_menu() {
    clear
    greenline "————————————————————————————————————————————————————"
    echo "
    ***********  DIY docker轻服务器  ***************
    CPU架构: $ARCH
    操作系统: $OS
    脚本作用:快速部署一个省电无感的小透明轻服务器
            --- Made by zhukaiyuan with YOU ---"
    echo -e "    https://gitee.com/yookoo/shell"
    greenline "————————————————————————————————————————————————————"
    echo "请选择操作："

    # 特殊处理的项数组
    special_items=("安装小雅tvbox" "安装特斯拉伴侣TeslaMate")
    for i in "${!menu_options[@]}"; do
        if [[ " ${special_items[*]} " =~ " ${menu_options[i]} " ]]; then
            # 如果当前项在特殊处理项数组中，使用特殊颜色
            cyan "$((i + 1)). ${menu_options[i]}"
        else
            # 否则，使用普通格式
            echo "$((i + 1)). ${menu_options[i]}"
        fi
    done
}


handle_choice() {
    local choice=$1
    # 检查输入是否为空
    if [[ -z $choice ]]; then
        echo -e "${RED}输入不能为空，请重新选择。${NC}"
        return
    fi

    # 检查输入是否为数字
    if ! [[ $choice =~ ^[0-9]+$ ]]; then
        echo -e "${RED}请输入有效数字!${NC}"
        return
    fi

    # 检查数字是否在有效范围内
    if [[ $choice -lt 1 ]] || [[ $choice -gt ${#menu_options[@]} ]]; then
        echo -e "${RED}选项超出范围!${NC}"
        echo -e "${YELLOW}请输入 1 到 ${#menu_options[@]} 之间的数字。${NC}"
        return
    fi

    # 执行命令
    if [ -z "${commands[${menu_options[$choice - 1]}]}" ]; then
        echo -e "${RED}无效选项，请重新选择。${NC}"
        return
    fi

    "${commands[${menu_options[$choice - 1]}]}"
}

while true; do
    show_menu
    read -p "请输入选项的序号(输入q退出): " choice
    if [[ $choice == 'q' ]]; then
        break
    fi
    handle_choice $choice
    echo "按任意键继续..."
    read -n 1 # 等待用户按键
done